<?php

/*
 * 标签请在配置文件中 'TAGLIB_BUILD_IN' => 'cx,Yibang'//自定义标签
 */
class TagLibTong extends TagLib{
	
	/*
	*  标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
	*/
    protected $tags = array(
        'sql' => array('attr'=>'sql','level'=>3,'close' => 1),
        'arclist' => array('attr'=>'table,field,limit,order,typeid,_sql','level'=>3,'close' => 1),
		'type' => array('attr'=>'table,field,limit,order,typeid,_sql','level'=>3,'close'=>1),
		'list' => array('attr'=>'table,field,limit,order,typeid,_sql','level'=>3,'close'=>1),
		'pagelist' => array('attr'=>'table,field,limit,order,typeid,_sql','level'=>3,'close'=>0),
		'likeart' => array('attr'=>'table,limit,order,typeid,_sql','level'=>3,'close'=>1),
        'position' =>array('attr'=>'_sql','level'=>3,'close'=>0),
		
    );
	
	/*
	* arclist标签 <arclist table="zixun" typeid="4" limit="0,1" flag="p" _sql="0" order="id desc">
	*/
    public function _sql($attr,$content){
        $tag = $this->parseXmlAttr($attr);
        if(!$tag['sql']) return false;
		if(!$tag['_sql']) $tag['_sql'] = 0;
        $str = <<<str
   <?php
       \$m = M();
        \$_result = \$m->query('{$tag["sql"]}');
		if({$tag['_sql']}) echo \$m->_sql();
        foreach(\$_result as \$k=>\$vo){
   ?>
str;
        $str.=$content;
        $str.='<?php }?>';
        return $str;        
    }
    
    
    /*
	* arclist标签 <arclist table="zixun" typeid="4" limit="0,1" flag="p" _sql="0" order="id desc">
	*/
    public function _arclist($attr,$content){
        $tag = $this->parseXmlAttr($attr);
        if(!$tag['table']) $tag['table'] = 'archive';
		if(!$tag['typeid']) $tag['typeid'] = 0;
		if(!$tag['_sql']) $tag['_sql'] = 0;
		if(!$tag['flag']) $tag['flag'] = 0;
                if(!$tag['order']) $tag['order'] = 'id desc';
                if($tag['typeid'])$ids = $this->get_typeids($tag['typeid']);
        $str = <<<str
   <?php
		unset(\$_where);
		if({$tag['flag']}) \$_where['flag'] = array('in','{$tag["flag"]}');
                if({$tag['typeid']}) \$_where['typeid'] = array('in','{$ids}');
       \$m = M("$tag[table]");
        \$_result = \$m->where(\$_where)->limit('{$tag["limit"]}')->order('{$tag["order"]}')->select();
		if({$tag['_sql']}) echo \$m->_sql();
        foreach(\$_result as \$k=>\$vo){
   ?>
str;
        $str.=$content;
        $str.='<?php }?>';
        return $str;        
    }
	
	/*
	* type标签 <type typeid="4">
	*/
	public function _type($attr,$content){
		$tag = $this->parseXmlAttr($attr);
		if(!$tag['table']) $tag['table'] = 'zixuntype';
		if(!$tag['_sql']) $tag['_sql'] = 0;
		$str = <<<str
   <?php
		unset(\$_where2);
		if({$tag['typeid']}) \$_where2['id'] = {$tag['typeid']};
       \$m = M("$tag[table]");
        \$_result = \$m->where(\$_where2)->limit('{$tag["limit"]}')->order('{$tag["order"]}')->select();
		if({$tag['_sql']}) echo \$m->_sql();
        foreach(\$_result as \$k=>\$vo){
   ?>
str;
        $str.=$content;
        $str.='<?php }?>';
        return $str;
	}
	
	/*
	* list标签 <list pagesize="24"></list>
	*/
	public function _list($attr,$content){
		$tag = $this->parseXmlAttr($attr);
		if(!$tag['table']) $tag['table'] = 'archive';
		if(!$tag['pagesize']) $tag['pagesize'] = '10';
		if(!$tag['typeid']) $tag['typeid'] = $_GET['id'];
                if(!$tag['order']) $tag['order'] = 'id desc';
		if(!$tag['_sql']) $tag['_sql'] = 0;
                if(!$tag['typeid']) $tag['typeid'] = 0;
                $ids = $this->get_typeids($_GET['id']);
		$str = <<<str
   <?php
		unset(\$_where);
                if({$tag['typeid']}) \$_where['typeid'] = array('in','{$ids}');
        \$m = M("$tag[table]");
	    \$_count = \$m->where(\$_where)->count();
		\$p = new Page(\$_count,{$tag['pagesize']});
		\$_result = \$m->where(\$_where)->limit("\$p->firstRow,\$p->listRows")->order('id desc')->select();
		if({$tag['_sql']}) echo \$m->_sql();
        foreach(\$_result as \$k=>\$vo){
   ?>
str;
        $str.=$content;
        $str.='<?php }?>';
        return $str;  
	}
	
	/*
	* pagelist 分页标签 <pagelist />
	*/
	public function _pagelist($attr){
		$str = <<<str
		   <?php
				if(\$p->show()){
					echo \$p->show();
					unset(\$p);
				}
		   ?>
str;
        return $str;
	}
	
	/*
	* likeart 相关文章标签 <likeart row="8"></likeart>
	*/
	public function _likeart($attr,$content){
		$tag = $this->parseXmlAttr($attr);
		$archive = C('archive');
		$_keyword = $archive['keywords'];
		$keywords = explode(',' , trim($_keyword));
		$keyword = '';
		$n = 1;				
		foreach($keywords as $k)
		{
			if($n > 3)  break;
			 
			if(trim($k)=='') continue;
			else $k = addslashes($k);
			 
			$keyword .= $keyword=='' ? " CONCAT(keywords,' ',title) LIKE '%$k%' " : " OR CONCAT(keywords,' ',title) LIKE '%$k%'";
			$n++;
		}
		if(!$keyword) return false;
		$keyword = '('.$keyword.') and (id <> '.$archive['id'].') ';
		if(!$tag['table']) $tag['table'] = 'archive';
		if(!$tag['limit']) $tag['limit'] = '0,10';
		if(!$tag['_sql']) $tag['_sql'] = 0;
		$str = <<<str
   <?php
		unset(\$_where);
		\$_where = "{$keyword}";
        \$m = M("$tag[table]");
		\$_result = \$m->where(\$_where)->limit("{$tag['limit']}")->select();
		if({$tag['_sql']}) echo \$m->_sql();
        foreach(\$_result as \$k=>\$field){
   ?>
str;
        $str.=$content;
        $str.='<?php }?>';
        return $str; 
	}
        
        /*
         * position 当前位置标签 <position />
         */
        public function _position($attr){
            $action = MODULE_NAME;
            if(strtolower($action) == 'archive'){
                $typeid = M('archive')->getFieldById($_GET['id'],'typeid');
            }elseif(strtolower($action) == 'arctype'){
                $typeid = $_GET['id'];
            }
            if(!$typeid)return;
             $tree = $this->position_type_tree($typeid);
             krsort($tree);
             foreach($tree as $v){
                 $html .= '<a href="'.U('Home/Arctype/index',array('id'=>$v['id'])).'">'.$v['typename'].'</a>  >  ';
             }
            return $html;
        }
	
        /*
         * 位置栏目树，递归
         */
        protected function position_type_tree($typeid){
            static $aa = array();
            $typeid = M('arctype')->where('id='.$typeid)->find();
            if(is_array($typeid)){
                $aa[] = $typeid;
                $this->position_type_tree($typeid['topid']);
            }
            return $aa;
        }
   
   /*
    * 得到当前栏目下的所有栏目ID，包括当前栏目ID
    */
   public function get_typeids($id){
       $m = M('arctype');
       $list = $m->field('id,topid')->order('topid asc,id asc')->select();
       $sub = Tool::tree($list,$id);
       if($sub){
            foreach($sub as $v){
                $ids[] = $v['id'];
            }
       }
       $ids[] = $id;
       $str = implode(',',$ids);
       return $str;
   }
   
    
}